<template>
	<xn-form-container
		:title="formData.id ? '编辑库位信息' : '增加库位信息'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="库位编号：" name="locationCode">
				<a-input v-model:value="formData.locationCode" placeholder="请输入库位编号" allow-clear/>
			</a-form-item>
			<a-form-item label="库位名称：" name="locationName">
				<a-input v-model:value="formData.locationName" placeholder="请输入库位名称" allow-clear/>
			</a-form-item>
			<a-form-item label="所属门店：" name="locationStore">
				<xn-page-select
					ref="xnlocationStorePageSelectRef"
					v-model:value="formData.locationStore"
					placeholder="请选择所属门店"
					allow-clear
					show-search
					:page-function="locationStoreSelector"
					:echo-function="locationStoreEcho"
					@change="setStoreageData(formData.locationStore, 0)"
				/>
			</a-form-item>
			<a-form-item label="所属仓库：" name="locationStorage">
				<xn-page-select
					ref="xnlocationStoragePageSelectRef"
					v-model:value="formData.locationStorage"
					placeholder="请选择所属仓库"
					allow-clear
					show-search
					:disabled="!formData.locationStore"
					:page-function="locationStorageSelector"
					:echo-function="locationStorageEcho"
				/>
			</a-form-item>
			<a-form-item label="排序码：" name="sortCode">
				<a-input v-model:value="formData.sortCode" placeholder="请输入排序码" allow-clear/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="supplierStoresLocationForm">
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import supplierStoresLocationApi from '@/api/supplier/warehouse/supplierStoresLocationApi'
import supplierStoresApi from "@/api/supplier/base/supplierStoresApi";
import supplierStoragesApi from "@/api/supplier/warehouse/supplierStoragesApi";
// 抽屉状态
const open = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const xnlocationStorePageSelectRef = ref();
const locationStoreSelector = (param) => {
	return supplierStoresApi.supplierStoresPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.storeName,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}
const locationStoreEcho = (param) => {
	let echoList = []
	let echo =  {}
	for(var i=0;i<param.idList[0].length;i++){
		echo.name = formData.value.locationStoreName
		echo.id = param.idList[0][i]
		echoList.push(echo)
	}
	return Promise.resolve(echoList)
}
const xnlocationStoragePageSelectRef = ref();
const locationStorageSelector = (param) => {
	return supplierStoragesApi.supplierStoragesPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.storeName,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}
const locationStorageEcho = (param) => {
	let echoList = []
	let echo =  {}
	for(var i=0;i<param.idList[0].length;i++){
		echo.name = formData.value.locationStorageName
		echo.id = param.idList[0][i]
		echoList.push(echo)
	}
	return Promise.resolve(echoList)
}
// 门市选择后查询对应的仓库
const setStoreageData = (storeId, type) => {
	if (storeId) {
		const xnStoragePageSelectParam = {
			parentId: storeId
		}
		xnlocationStoragePageSelectRef.value.onPage(xnStoragePageSelectParam);
		formData.value.locationStorage = undefined
		// 此类型代表选择的时候重置后面的职位
	} else {
		formData.value.locationStore = undefined
		formData.value.locationStorage = undefined
	}
}

// 打开抽屉
const onOpen = (record) => {
	open.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	}
	nextTick(() => {
		xnlocationStorePageSelectRef.value.onPage({current: 1});
	});
	nextTick(() => {
		xnlocationStoragePageSelectRef.value.onPage({current: 1,parentId: formData.value.locationStore});
	});
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	open.value = false
}
// 默认要校验的
const formRules = {}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			supplierStoresLocationApi
				.supplierStoresLocationSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
		.catch(() => {
		})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
